package com.fawvwebanma.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fawvwebanma.api.BuyTicketApi;
import com.fawvwebanma.dao.OrderListDao;
import com.fawvwebanma.dao.TicketDao;
import com.fawvwebanma.dto.PersonDto;
import com.fawvwebanma.dto.Result;
import com.fawvwebanma.dto.ResultData;
import com.fawvwebanma.dto.TicketDto;
import com.fawvwebanma.entity.OrderList;
import com.fawvwebanma.entity.Person;
import com.fawvwebanma.entity.Ticket;
import com.fawvwebanma.service.IBuyTicketsService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jieye.ren
 * @date 2021/3/11
 */
@Component
@DubboService
@Slf4j
public class BuyTicketsService implements IBuyTicketsService {
    @DubboReference(version = "${dubbo.provider.version}", check = false, application = "${dubbo.application.id}")
    BuyTicketApi buyTicketApi;
    @Autowired
    TicketDao ticketDao;
    @Autowired
    OrderListDao orderListDao;

    @Override
    public Result<List<TicketDto>> buyTicket(String userId, String trainNum, String leaveStationName,
        String arriveStationName, String depatureDate, List<Person> persons) {
        List<PersonDto> personDtos = new ArrayList<>();
        String orderNum = "";
        if (persons == null) {
            return Result.error("0", "请先添加乘客信息......");
        }
        for (Person person : persons) {
            PersonDto personDto =
                new PersonDto(person.getPersonId(), person.getUserId(), person.getName(), person.getTel());
            personDtos.add(personDto);
        }
        ResultData<List<TicketDto>> resultData =
            buyTicketApi.buyTicket(trainNum, leaveStationName, arriveStationName, depatureDate, personDtos);
        for (TicketDto ticketDto : resultData.getData()) {
            Ticket ticket = new Ticket();
            ticket.setPersonId(ticketDto.getPersonId());
            ticket.setOrderNum(ticketDto.getOrderNum());
            ticket.setTrainNum(ticketDto.getTrainNum());
            ticket.setDepatureDate(ticketDto.getDepatureDate());
            ticket.setCarriageNum(ticketDto.getCarriageNum());
            ticket.setSeatNum(ticketDto.getSeatNum());
            ticket.setSeatLevel(ticketDto.getSeatLevel());
            ticket.setLeaveStationName(ticketDto.getLeaveStationName());
            ticket.setArriveStationName(ticketDto.getArriveStationName());
            ticket.setTicketType(ticketDto.getTicketType());
            ticket.setTicketPrice(ticketDto.getTicketPrice());
            ticket.setStatus(0);
            ticketDao.saveTickets(ticket);
            orderNum = ticketDto.getOrderNum();
        }

        // 保存订单状态
        OrderList orderList = new OrderList(orderNum, userId, new Date(), 0);
        orderListDao.saveOrderList(orderList);

        return Result.success(resultData.getData());
    }
}
